<template>
	<view>
		<simple-toolbar :title="$t('pool.poolInfo.newPoolInfo')">
			<view class="returnToConfirm">
				<el-button @click="navBack">{{ $t('global.action.back') }}</el-button>
				<el-button @click="confirm" type="primary" :disabled="success">{{ $t('global.action.confirm') }}</el-button>
			</view>
		</simple-toolbar>

		<view class="pad-base form">
			<el-form :model="formData" :rules="rules" ref="formData" label-position="top">
				<el-form-item :label="$t('pool.poolInfo.pool_name')" prop="pool_name">
					<el-input v-model="formData.pool_name" :placeholder="$t('pool.poolInfo.pool_name_pla')">
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('pool.poolInfo.public_key')" prop="public_key">
					<el-input v-model="formData.public_key" :placeholder="$t('pool.poolInfo.public_key_pla')">
					</el-input>
				</el-form-item>
				<!-- <el-form-item :label="$t('pool.poolInfo.wallet')" prop="wallet">
					<el-input v-model="formData.wallet" :placeholder="$t('pool.poolInfo.wallet_pla')"></el-input>
				</el-form-item> -->
				<el-form-item :label="$t('pool.poolInfo.total_tib')" prop="total_tib">
					<el-input v-model="formData.total_tib" type="number" :placeholder="$t('pool.poolInfo.total_tib_pla')">
						<template slot="suffix">TiB</template>
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('pool.poolInfo.current_tib')" prop="current_tib">
					<el-input v-model="formData.current_tib" type="number" :placeholder="$t('pool.poolInfo.current_tib_pla')">
						<template slot="suffix">TiB</template>
					</el-input>
				</el-form-item>
			</el-form>
		</view>
	</view>
</template>

<script>
	import {
		createPoolInfo,
		updatePoolInfo
	} from "../../../js_sdk/uni-admin/assetPool.js"

	export default {
		onLoad(option) {
			if (option.data) {
				this.formData = JSON.parse(decodeURIComponent(option.data));
			}
		},
		data() {
			return {
				success: false,
				formData: {
					pool_name: '',
					public_key: '',
					// wallet: '',
					current_tib: '',
					total_tib: '',
				},
				rules: {
					pool_name: [{
						required: true,
						// message: '请输入公钥',
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('pool.poolInfo.pool_name_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					public_key: [{
						required: true,
						// message: '请输入公钥',
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('pool.poolInfo.public_key_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					// wallet: [{
					// 	required: true,
					// 	// message: '请输入钱包地址',
					// 	validator: (rule, value, callback) => {
					// 		if (value === '') {
					// 			callback(new Error(this.$t('pool.poolInfo.wallet_pla')))
					// 		} else {
					// 			callback()
					// 		}
					// 	},
					// 	trigger: 'blur'
					// }],
					current_tib: [{
						required: true,
						// message: '请输入当前算力',
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('pool.poolInfo.current_tib_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					total_tib: [{
						required: true,
						// message: '请输入历史总挖矿量',
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('pool.poolInfo.total_tib_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
				}
			}
		},
		methods: {
			confirm() {
				this.$refs.formData.validate(async valid => {
					if (!valid) return
					let data = Object.assign({}, this.formData)
					data.total_tib = parseFloat(data.total_tib)
					data.current_tib = parseFloat(data.current_tib)
					if (this.formData.id) {
						delete data.id
						await updatePoolInfo(this.formData.id, data)
					} else {
						await createPoolInfo(data)
					}
					this.$message.success('保存成功')
					this.success = true
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				});
			},
			navBack() {
				uni.navigateBack()
			}
		},
	}
</script>

<style>

</style>
